package com.sangmo.fts.process.inventory.service;

import com.sangmo.boot.framework.common.utils.DateUtils;
import com.sangmo.boot.framework.data.idm.IdentityManager;
import com.sangmo.boot.framework.plugin.dataview.crud.service.BaseViewService;
import com.sangmo.boot.framework.plugin.dataview.data.IDataViewRepo;
import com.sangmo.fts.basic.service.FileUploadService;
import com.sangmo.fts.common.authority.service.AuthorityEntityService;
import com.sangmo.fts.process.inventory.interfaces.IfContractCreation;
import com.sangmo.fts.process.inventory.model.IfContract;
import com.sangmo.fts.process.inventory.repo.IfContractRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
public class IfContractEntityService extends AuthorityEntityService<IfContract> {

    @Autowired
    private IfContractRepo ifContractRepo;

    @Autowired
    private IdentityManager identityManager;

    @Autowired
    private FileUploadService fileUploadService;

    @Override
    protected IDataViewRepo<IfContract> getRepo() {
        return ifContractRepo;
    }

    @Override
    protected void onCreate(IfContract entity, Object bo) {
        super.onCreate(entity, bo);
        entity.setCode(DateUtils.format(new Date(), "yyyyMMddHHmmss") + identityManager.next("IF_CONTRACT", 4));
    }

    @Override
    protected void afterCreated(IfContract entity, Object bo) {
        super.afterCreated(entity, bo);
        IfContractCreation creation = (IfContractCreation) bo;
        if (null != creation.getPledgorDoc()) {
            fileUploadService.updateRef(creation.getPledgorDoc(), ""+entity.getId(), "IfContract.Pledgor");
        }
    }

    protected void pledgorConfirm(IfContract entity) {
        entity.setPledgorConfirm(true);
        this.save(entity);
    }

    protected void pledgeeConfirm(IfContract entity) {
        entity.setPledgeeConfirm(true);
        this.save(entity);
    }

    protected void regulatorConfirm(IfContract entity) {
        entity.setRegulatorConfirm(true);
        this.save(entity);
    }
}

